import { EVENT_BUS_KEY } from '../../common/constants';
import {
    getCurrentPlayList,
    getCurrentLesson
} from '../../common/service/courses-service';
import { eventBusGetter } from '../../utils/eventbus';
import page from '../../common/service/page-service';
// import { EVENT_PLAY_START, EVENT_PLAY_PAUSE, EVENT_PLAY_PREV, EVENT_PLAY_NEXT, EVENT_PLAY_ENDED} from '../../components/zh-audio/zh-audio';

Page({
    /**
   * 页面的初始数据
   */
    data: {
        playList: [],
        lessonsCount: 0,
        courseType: 1,
        courseTitle: '课程列表',
        purchaseStatus: 1
    },

    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function (options) {
        this.data.lessonsCount = parseInt(options.lessonsCount, 10);
        this.data.courseType = parseInt(options.courseType, 10);
        this.data.purchaseStatus = parseInt(options.purchaseStatus, 10);
        if (options.courseTitle.length > 0) {
            page.setNavigationBarTitle({
                title: options.courseTitle
            });
        }
    },

    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function () {
        this.onLoadData();
    },

    onUnload: function () {},

    /**
   * 从本地拿列表数据
   */
    onLoadData: function () {
        let that = this;
        getCurrentPlayList().then(res => {
            console.log(res);
            that.changeCoureseStatus(res);
        });
    },

    /**
   * 播放按钮点击
   */
    onItemTap: function (e) {
        eventBusGetter().$emit(EVENT_BUS_KEY.PLAY_LESSON_UPDATE, {
            audioInfo: e.detail
        });
        page.navigateBack({});
    },

    /**
   * 改变课程显示状态
   */
    changeCoureseStatus (list) {
        let coureseList = [];
        let currentLesson = getCurrentLesson({ isSync: true });
        for (let i = 0; i < list.length; i++) {
            let item = list[i];
            if (item.lessonId === currentLesson.lessonId) {
                item.isPlaying = true;
            } else {
                item.isPlaying = false;
            }
            coureseList.push(item);
        }
        this.setData({
            playList: coureseList,
            lessonsCount: this.data.lessonsCount,
            courseType: this.data.courseType,
            purchaseStatus: this.data.purchaseStatus
        });
    }
});
